package com.acme.chat.database;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;
import java.sql.Statement;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

import com.acme.chat.client.ChatClient;

public class AdapterDB extends JFrame implements ActionListener {
	private MyConnect con;
	private JButton close = new JButton("CLOSE");
	private JButton ok = new JButton("OK");
	private JTextField url = new JTextField("localhost", 15);
	private JTextField port = new JTextField("3306", 15);
	private JTextField nameDB = new JTextField("MessageHistory", 12);
	private JTextField user = new JTextField("root", 10);
	private JPasswordField password = new JPasswordField("8701", 11);

	public AdapterDB() {
		super("DataBase Connection.");
		setDefaultCloseOperation(DISPOSE_ON_CLOSE);
		close.addActionListener(this);
		ok.addActionListener(this);
		add(new JLabel("Connnection info: "), BorderLayout.NORTH);

		JPanel centerPanel = new JPanel();
		centerPanel.add(new JLabel("URL:"));
		centerPanel.add(url);
		centerPanel.add(new JLabel("Port:"));
		centerPanel.add(port);
		centerPanel.add(new JLabel("Name DB:"));
		centerPanel.add(nameDB);
		centerPanel.add(new JLabel("User Name:"));
		centerPanel.add(user);
		centerPanel.add(new JLabel("Password:"));
		centerPanel.add(password);
		add(centerPanel, BorderLayout.CENTER);
		JPanel southPanel = new JPanel();
		southPanel.add(ok);
		southPanel.add(close);
		add(southPanel, BorderLayout.SOUTH);
		setVisible(true);
		setSize(230, 200);
		setResizable(false);
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		Object o = e.getSource();
		if (o == ok) {
			con = new MyConnect(url.getText(), port.getText(),
					nameDB.getText(), user.getText(), password.getText());

			con.startConnection();
			url.setEditable(false);
			port.setEditable(false);
			nameDB.setEditable(false);
			user.setEditable(false);
			password.setEditable(false);
			ok.setEnabled(false);
		}
		if (o == close) {
			if (con != null) {
				con.closeConnection();
			}
			System.out.println("Exit.");
			url.setEditable(true);
			port.setEditable(true);
			nameDB.setEditable(true);
			user.setEditable(true);
			password.setEditable(true);
			ok.setEnabled(true);
		}
	}

	public String getPrivateMessageHistory(String username) {
//		Statement stat = con.getStatement();
//		StringBuilder sbuild = new StringBuilder();
//		try {
//			ResultSet r = stat
//					.executeQuery("SELECT * FROM Archive WHERE from_cl='"
//							+ username + "';");
//			while (r.next()) {
//				sbuild.append("To: " + r.getString(2) + "\n");
//				sbuild.append("TIME/DATE: " + r.getString(3) + "/"
//						+ r.getString(4));
//				sbuild.append("\nmes: " + r.getString(5) + '\n');
//
//			}
//		} catch (SQLException e) {
//			System.out.println("Try execute query error!");
//		}
		return null;
	}

	public void savePrivateMessage(ChatClient from, ChatClient to, String time,
			String sdate, String message) {
//		String mymessage = message.substring(0, message.length() - 1);
//		Statement stat = con.getStatement();
//		try {
//			stat.executeUpdate("INSERT INTO Archive(from_cl,to_cl,time_send,date_send,message) VALUE('"
//					+ from.getName()
//					+ "','"
//					+ to.getName()
//					+ "','"
//					+ time.toString()
//					+ "','"
//					+ sdate.toString()
//					+ "','"
//					+ mymessage + "');");
//		} catch (SQLException e) {
//			System.out.println("Try update fail...");
//			e.printStackTrace();
//		}
	}

	public void deleteAllFromDB() {
		Statement stat = con.getStatement();
		try {
			stat.executeUpdate("DELETE * FROM Archive;");
		} catch (SQLException e) {
			System.out.println("Try update fail...");
			e.printStackTrace();
		}
	}

	public static void main(String[] args) {
		new AdapterDB();
	}
}